<?php
/**
 * @author spenceryg
 * @date 2022/2/6
 * @time 4:37 下午
 * @focus love & life
 */


namespace app\common\lib;


use ReflectionClass;
use ReflectionMethod;

class SpClassMethods
{

    /**
     * 获取类的public方法
     * @param $class_name
     * @param string $model
     * @return array
     * @throws \ReflectionException
     */
    public static function getClassPublicMethods($class_name, string $model = 'admin'): array
    {
        $this_class = "app\\". str_replace('/',"\\" ,$model) . "\controller\\". str_replace('/',"\\" ,$class_name);
        $file_name = "../app/" . $model ."/controller/" . $class_name . '.php';
        $reflect = new ReflectionClass($this_class);
        $class_comment = $reflect->getDocComment();
        $class_comment = explode("\n", $class_comment)[1];
        $class_comment = trim($class_comment);
        $class_comment = trim($class_comment, '*');
        $class_comment = trim($class_comment);
        $method_arr = $reflect->getMethods(ReflectionMethod::IS_PUBLIC);
        $res = [];
        foreach ($method_arr as $v){
            if($v->class == $this_class){
                $reflect_method = new ReflectionMethod($this_class, $v->name);
                $comment = $reflect_method->getDocComment();
                $comment = explode("\n", $comment)[1];
                $comment = trim($comment);
                $comment = trim($comment, '*');
                $comment = trim($comment);
                $start = $reflect_method->getStartLine() - 1;
                $end = $reflect_method->getEndLine();
                $content = FileWrapper::get_line($file_name, $start, $end);
                //不体现构造函数之类的系统函数
                if(strpos($v->name, '__')===false){
                    $res[] = [
                        'name' => $v->name,
                        'url' => '/admin/' . str_replace('/',"." ,$class_name) . '/' . $v->name,
                        'comment' => $class_comment . '-' . $comment,
                        'content' => $content,
                        'start_line' => $start,
                        'end_line' => $end
                    ];
                }

            }
        }
        return $res;
    }
}
